<?php include '../../checkPCandVPC.php'; ?>
<?php include 'top.html'; ?>
<?php include 'notifier.php'; ?>
<?php include '../../authorization_check.php'; ?>

<?php

require_once '../../commonutils/util/PageTemplate.php';
require_once '../../commonutils/db/DBConnectionManager.php';


//global variables
$totalPayment = 0;
$count = 0;
$dcount = 0;
$totalCredit = 0;
$totalPayment = 0;
$depSC;
$bosrow = 0;
//$dep; 

function listBudgetForm($webPage){
$webPage->addContent("<form name='BudgetForm' method='post' action=''>");				
}

//FIND THE NUMBER OF RECORDS AND NUMBER OF DIFFERENT DEPARTMENTS GLOBALY

$result = DBConnectionManager::query("SELECT DISTINCT
  enrollment.studentID,
  enrollment.semesterID,
  enrollment.sectionID,
  course.courseName,
  course.courseCode,
  student.lastName,
  student.firstName,
  section.semesterID,
  section.sectionID,
  section.courseCode,
  student.programCode,
  course.credit,
  program.longName,
  program.programCode,
  student.studentID
FROM
  enrollment,
  course,
  student,
  section,
  program
WHERE
  (enrollment.studentID = student.studentID) AND
  (enrollment.semesterID = section.semesterID) AND
  (enrollment.sectionID = section.sectionID) AND
  (student.programCode = program.programCode) AND
  (section.courseCode = course.courseCode) AND
  (student.programCode <> '904') AND
  (section.courseCode LIKE '9040%')
GROUP BY
  enrollment.studentID,
  enrollment.semesterID,
  enrollment.sectionID,
  section.courseCode
ORDER BY
  student.programCode
");

		$row = mysql_fetch_assoc($result);
		$dep[$dcount] = $row['programCode'];
		$depN[$dcount] = $row['longName'];
		//$names = $row['firstName'];
		//echo "$names";
		$depSC[$dcount] = 1;
		while($row = mysql_fetch_assoc($result)){
		//$names2 = $row['firstName'];
		//echo "$names2";
			if( $row['programCode'] != $dep[$dcount] ){
          $dcount ++;
          $depSC[$dcount] = 0;
          //echo "depSC[".$dcount."]=".$depSC[$dcount]."<br>";
					$dep[$dcount] = $row['programCode'];
					$depN[$dcount] = $row['longName'];
					}			
			$depSC[$dcount]++;
			echo "depSC[".$dcount."]=".$depSC[$dcount]."<br>";
		}
	/*echo "onceki dep: ";
	echo "$dep[0]";
	echo "ustteki count: ";
	echo "$count"+1;
	echo "ustteki dcount: ";
	echo "$dcount"+1;*/

function listBudget($webPage, $loopNo){
	
	global $count, $dcount, $dep, $depN, $totalCredit, $totalPayment,$bosrow, $depSC ;
	$totalCredit = 0;
	$totalPayment = 0;
	//echo "loopNo:"."$loopNo";
	
	//echo "sonraki dep: ";
	//echo "$dep[0]";
		
	$webPage->addContent("<form name='listBudgetForm' method='post'>");
	$webPage->addContent("<table align = 'center' border=1 name='listBudgetTable'>");
	$webPage->addContent("<tr>");
		$webPage->addContent("<th>Student ID</th>");
		$webPage->addContent("<th>Name</th>");
		$webPage->addContent("<th>Surname</th>");
		$webPage->addContent("<th>Department</th>");
		$webPage->addContent("<th>Course Code</th>");
		$webPage->addContent("<th>Course Name</th>");
		$webPage->addContent("<th>Course Credit</th>");
		$webPage->addContent("<th>Total Payment (YTL)</th>");
	$webPage->addContent("</tr>");
	
	//echo "dep[loopNo]: ";
	//echo "$dep[$loopNo]";
	
		$result = DBConnectionManager::query("SELECT DISTINCT
  enrollment.studentID,
  enrollment.semesterID,
  enrollment.sectionID,
  course.courseName,
  course.courseCode,
  student.lastName,
  student.firstName,
  section.semesterID,
  section.sectionID,
  section.courseCode,
  student.programCode,
  course.credit,
  program.longName,
  program.programCode,
  student.studentID
FROM
  enrollment,
  course,
  student,
  section,
  program
WHERE
  (enrollment.studentID = student.studentID) AND
  (enrollment.semesterID = section.semesterID) AND
  (enrollment.sectionID = section.sectionID) AND
  (student.programCode = program.programCode) AND
  (section.courseCode = course.courseCode) AND
  (student.programCode <> '904') AND
  (section.courseCode LIKE '9040%')
GROUP BY
  enrollment.studentID,
  enrollment.semesterID,
  enrollment.sectionID,
  section.courseCode
ORDER BY
  student.programCode
"); 
	//echo "for oncesi: ";
	//echo "$count"+1;
	$bosrow = 0;
	echo "loopNo:".$loopNo;
	for($l=0; $l < $loopNo ; $l++){
	echo "xxxxx".$depSC[$l]."<br>";
	$bosrow = $bosrow + $depSC[$l];
	echo "ehuehuehubosRow:".$bosrow."<br>";
	//for($k =0; $k <= $depSC[$l]; $k++){
  //  $bosrow ++;
  //  echo $k;
  //  echo $bosrow;
	//}
	}
	
	
	for($i =0 ; $i <= $depSC[$loopNo] ; $i++){
			$row = mysql_fetch_assoc($result);
			if($i <= $bosrow)
			{
			}
			
			else{
			
			$studentID = $row['studentID'];
			//echo "$studentID"." "; 
			$firstName = $row['firstName']; 
			//echo "$firstName"." "; 
			$lastName = $row['lastName']; 
			//echo "$lastName"." "; 
			$department = $row['longName'];
			//echo "$department"." "; 
			$courseCode = $row['courseCode'];
			//echo "$courseCode"." "; 
			$courseName = $row['courseName'];
			//echo "$courseName"." "; 
			$credit = $row['credit'];
			//echo "$credit"." "; 
			$totalCredit +=$credit;
			//echo "$totalCredit"." "; 
			$totalStudentPayment = 300 * $credit;
			//echo "$totalStudentPayment"." "; 
			$totalPayment += $totalStudentPayment;
			//echo "$totalPayment"."<br>"; 
			$webPage->addContent("<tr>");
				$webPage->addContent("<td>$studentID</td>");
				$webPage->addContent("<td>$firstName</td>");
				$webPage->addContent("<td>$lastName</td>");
				$webPage->addContent("<td>$department</td>");
				$webPage->addContent("<td>$courseCode</td>");
				$webPage->addContent("<td>$courseName</td>");
				$webPage->addContent("<td align = 'center'>$credit</td>");
				$webPage->addContent("<td align = 'center'>$totalStudentPayment</td>");
				//$webPage->addContent("<td>$totalPaymentForACourse</td>");	
			$webPage->addContent("</tr>");
			//$count++;
		}
	
	$webPage->addContent("<tr><td colspan = 6/></td><th align= left><u>Total Payment</u></th><th align=center>$totalPayment</th>");
	$webPage->addContent("<tr><td colspan = 6/></td><th align= left><u>Total Credit</u></th><th align=center>$totalCredit</th>");
	$webPage->addContent("</tr>");
	$webPage->addContent("</table>");
	$webPage->addContent("<table align = 'center'><tr>");
	$webPage->addContent("<td>");
	$webPage->addContent("</td>");
	$webPage->addContent("<td>");
	$webPage->addContent("</td>");
	$webPage->addContent("</tr></table></form>");
	}
}


//BURASI SAYFANIN BASLIGININ VE FOOTER'ININ OLUSTURULDUGU YER

$webPage = new Page();


//addJavaScripts($webPage);

$webPage->addHeader('Non-ION Students Taking Informatics Online (ION) Courses Details');

//$lc = $dcount + 1;
//echo "en alttaki lc: ";
//echo "$lc";
	
listNonProgStudentsForm($webPage);

	$dc = $dcount + 1;
	echo "en alttaki dcount: ";
	echo "$dc";
	echo "<br>";
	echo $bosrow;

if($dc == 1){
		$webPage->addCenterContent("!!! THERE ISN'T ANY NON-ION STUDENT TAKING ION COURSES !!!");
	}else{
for($j = 0; $j < $dc; $j++){
		//echo "j: "."$j";
		$webPage->addCenterContent("Students from $depN[$j] Department");
		listStudents($webPage,$j);		
		}
	}
$webPage->addFooter(date('Y'), 'IIPIS');
echo $webPage->get();
include 'bottom.php';
?>